﻿using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.MSSQLDAL
{
    public class SysParamDAL : UcAsp.Net.IDAL.ISysParamDAL
    {
        public SysParam Insert(SysParam model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.SysParams.Add(model);
                entities.SaveChanges();
                return model;
            }
        }

        public bool Update(SysParam model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                try
                {
                    System.Data.Entity.Infrastructure.DbEntityEntry<SysParam> entry = entities.Entry<SysParam>(model);
                    entry.State = System.Data.EntityState.Modified;
                    entities.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Delete(SysParam model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<SysParam> entry = entities.Entry<SysParam>(model);
                entry.State = System.Data.EntityState.Deleted;
                entities.SaveChanges();
                return true;
            }
        }

        public List<SysParam> GetListByGroupID(int id)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.SysParams.Where(o => o.GroupID == id).ToList();
            }
        }

        public SysParam GetParamByName(string name)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.SysParams.FirstOrDefault(o => o.ParamName == name);
            }
        }

        public SysParam GetParamByID(int id)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.SysParams.FirstOrDefault(o => o.ID == id);
            }
        }

        public List<SysParam> Select(int pagesize, int pageindex, out int pagecount)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<SysParam> list = new List<SysParam>();
                var query = entities.SysParams;
                pagecount = (query.Count() / pagesize) + 1;
                list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                return list;
            }
        }
    }
}